package com.ymxk.timestar.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

/**
 * 屏幕位移、
 */
public class LocationInScreenActivity extends AppCompatActivity {

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, LocationInScreenActivity.class);
        context.startActivity(intent);
    }

    private ConstraintLayout mMain;
    private Button mBtnMove;
    private TextView mTvCenter;
    private final String TAG = LocationInWindowActivity.class.getSimpleName();
    private Button mBtnGo;
    private int centerX;
    private int centerY;
    private int moveX;
    private int moveY;
    private LinearLayout mLlBtn;
    private Button mBtnMove1;
    private Button mBtnMove2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_location_in_screen);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        initView();

        mTvCenter.post(this::onInitCenterLocation);
//        mBtnMove.post(this::onInitButtonLocation);
        mBtnMove.post(() -> onInitButtonLocation(mBtnMove));
//        mBtnMove1.post(() -> onInitButtonLocation(mBtnMove1));
        //mBtnMove2.post(() -> onInitButtonLocation(mBtnMove2));

    }

    private void onInitButtonLocation(Button button) {
        int[] location = new int[2];
        button.getLocationOnScreen(location);
        /*moveX = location[0] + button.getWidth() / 2;
        moveY = location[1] + button.getHeight() / 2;*/
        moveX = location[0];
        moveY = location[1];
        Log.e(TAG, "onButtonMove: x=" + moveX + ",y=" + moveY);
        //onButtonMove: x=300,y=179
    }

    private void onInitCenterLocation() {
        int width = mTvCenter.getWidth();
        int height = mTvCenter.getHeight();
        Log.e(TAG, "onInitCenterLocation: w=" + width + "h=" + height);

        int[] location = new int[2];
        mTvCenter.getLocationOnScreen(location);
        /*centerX = location[0] + width / 2;
        centerY = location[1] + height / 2;*/
        centerX = location[0];
        centerY = location[1];
        Log.e(TAG, "onInitCenterLocation: x=" + centerX + ",y=" + centerY);
        // x=540,y=1188
    }

    private void initView() {
        mMain = findViewById(R.id.main);
        mBtnMove = findViewById(R.id.btnMove);
        mBtnMove.setOnClickListener(this::onButtonMove);
        mTvCenter = findViewById(R.id.tvCenter);
        mTvCenter.setOnClickListener(v -> onInitCenterLocation());

        mBtnGo = findViewById(R.id.btnGo);
        mBtnGo.setOnClickListener(this::moveToCenter);
        mLlBtn = (LinearLayout) findViewById(R.id.llBtn);
        mBtnMove1 = (Button) findViewById(R.id.btnMove1);
        mBtnMove2 = (Button) findViewById(R.id.btnMove2);
    }

    private void moveToCenter(View v) {
        int x = centerX - moveX;
        int y = centerY - moveY;
        mLlBtn.animate()
                .x(x)
                .y(y)
                .start();
    }

    private void translationToCenter(View v) {
        int x = centerX - moveX;
        int y = centerY - moveY;
        mLlBtn.animate()
                .translationX(x)
                .translationY(y)
                .start();
    }

    private void onButtonMove(View v) {
        Log.e(TAG, "onButtonMove: ");
        onInitButtonLocation((Button) v);
    }
}